package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //查询素有用户
    public PageResult findAll(Integer page, Integer pagesize) {
        List<UserInfo> list = userInfoApi.findAll(page, pagesize);
        for (UserInfo userInfo : list) {
            String key =Constants.USER_FREEZE+userInfo.getId();
            if(redisTemplate.hasKey(key)){
                userInfo.setUserStatus("2");
            }
        }
       Integer counts= userInfoApi.findCounts(page, pagesize);
        return new PageResult(page, pagesize, counts, list);
    }

    //查看用户信息//设置是否冻结
    public UserInfo findByUserInfo(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        String key =Constants.USER_FREEZE+userId;
        if(redisTemplate.hasKey(key)){
            userInfo.setUserStatus("2");
        }
        return userInfo;
    }

    public PageResult findByUserVideos(Integer page, Integer pagesize, Long uid) {
        List<Video> list = videoApi.findByUserVideos(page, pagesize, uid);
        UserInfo userInfo = userInfoApi.findById(uid);
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : list) {
            VideoVo init = VideoVo.init(userInfo, video);
            vos.add(init);
        }
        return new PageResult(page, pagesize, 0, vos);
    }

    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        PageResult pr = movementApi.findAllByMovements(page, pagesize, uid, state);
        List<Movement> items = (List<Movement>) pr.getItems();//获得动态数据
        if (CollUtil.isEmpty(items)) {
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(userIds, null, null);
        List<MovementsVo> vos  =new ArrayList<>();
        for (Movement movement : items) {
            UserInfo userInfo = userInfoMap.get(movement.getUserId());
            if(userInfo!=null){
                MovementsVo init = MovementsVo.init(userInfo, movement);
                vos.add(init);
            }
        }
           pr.setItems(vos);
        return pr;

    }

    public Map freezeUser(Map map) {
        Integer time =Integer.valueOf( map.get("freezingTime").toString());
        String userId =String.valueOf(map.get("userId"));

        String key = Constants.USER_FREEZE+userId;
        int day=0;
        if(time==1){
            day=3;
        }
        if(time==2){
            day=7;
        }
        String toJSONString = JSON.toJSONString(map);
            if(day>0){
                redisTemplate.opsForValue().set(key,toJSONString,day,TimeUnit.DAYS);
            }else {
                redisTemplate.opsForValue().set(key,toJSONString);
            }
            Map map1 =new HashMap();
            map1.put("message","操作成功");
            return map1;
    }

    public Map userUnfreeze(Map params) {
        String userId =String.valueOf(params.get("userId"));
        String key = Constants.USER_FREEZE+userId;
        redisTemplate.delete(key);
        Map map = new HashMap();
        map.put("message","解冻成功");
        return map;
    }

//    public List<VideoVo> findByUserVideos(Integer page, Integer pagesize, Long uid) {
//        List<Video> list=videoApi.findByUserVideos(page,pagesize,uid);
//        UserInfo userInfo = userInfoApi.findById(uid);
//        List<VideoVo> vos =new ArrayList<>();
//        for (Video video : list) {
//            VideoVo init = VideoVo.init(userInfo, video);
//            vos.add(init);
//        }
//return vos;
//    }
}
